﻿using Entitas;
using System.Linq;
class EnemyReturnTospawnRequestSystem : IExecuteSystem
{
    EnemyContext enemyContext;
    RequestContext requestContext;
    public EnemyReturnTospawnRequestSystem(Contexts contexts) 
    {
        enemyContext = contexts.enemy;
        requestContext = contexts.request;
    }


   public void Execute()
    {
        foreach (var e in enemyContext.GetEntities().Where(x=>x.isRoaming==false&&x.isBattleEngaged==false&&x.hasPosition==true))
        {
            if (e.position.value != e.spawnPosition.value)
            {
                ServiceManager.DebugMessageService.Print("return to spawn request being made");
                if (e.position.value.x > e.spawnPosition.value.x && e.position.value.y == e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Left);

                }
                else if (e.position.value.x > e.spawnPosition.value.x && e.position.value.y < e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Up_Left);

                }

                else if (e.position.value.x > e.spawnPosition.value.x && e.position.value.y > e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Down_Left);

                }

                else if (e.position.value.x < e.spawnPosition.value.x && e.position.value.y == e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Right);

                }
                else if (e.position.value.x < e.spawnPosition.value.x && e.position.value.y < e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Up_Right);

                }
                else if (e.position.value.x < e.spawnPosition.value.x && e.position.value.y > e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Down_Right);

                }

                else if (e.position.value.x == e.spawnPosition.value.x && e.position.value.y > e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Down);

                }
                else if (e.position.value.x == e.spawnPosition.value.x && e.position.value.y < e.spawnPosition.value.y)
                {
                    requestContext.CreateEntity().AddReturnToSpawnRequest(e.name.value, Direction.Up);

                }

                ServiceManager.DebugMessageService.Print("testing in" + ToString());
            }
        
        }
    }

    public override string ToString()
    {
        return "Enemy Return To Spawn System";
    }

}


